$(function(){
	$('#btn_menu_dir_add').click(function(){
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes =treeObj.getSelectedNodes(); 
        if(nodes[0] != null)
        {
	        if(nodes[0].menuType == 1 || nodes[0].menuType == 2)
	        {
	        	alert("菜单或者功能不能增加子目录");
	        	return ;
	        }
        }
        var parentName = "root";
        var parentId = "0";
        var menuType = "0";
        ebcForm.resetForm('#menudetail');
		$('#menuType').attr('value', menuType);
    	$('#parentmenuID').attr('value', parentId);
    	$('#parentMenuId').val(parentName);
    	$('#menu_id').val("");
    	$('#id').attr('value', null);	
    	$('#menuUrl').attr('value', null);	
    	$('#btn_menu_save').attr('disabled', false);

	});
	$('#btn_menu_add').click(function(){
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes =treeObj.getSelectedNodes(); 
        if(!nodes[0]){
        	alert("请先选择目录");
        	return;
        }
        if(nodes[0].isParent!=true){
        	alert("不能在子菜单下新建子菜单");
        	return;
        }
        ebcForm.resetForm('#menudetail');
        var menuType = "1";
        $('#menuType').attr('value', menuType);
        var treeNode = nodes[0];
        $('#parentMenuId').val(treeNode.name);
        $("#parentmenuID").val(treeNode.id);
        $("#sortOrder").val(nodes.length+1);
    	$('#btn_menu_save').attr('disabled', false);
	});
	$('#btn_menu_del').click(function(evt){
		
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes =treeObj.getSelectedNodes(); 
        if(nodes[0] == null)
        {
			eDialog.alert('请选择选择一个选项进行操作！');
        }
        else
	    {
			eDialog.confirm("您确认要删除该选项吗？", {
				buttons : [{
					caption : '确认',
					callback : function(){
						eDialog.alert('success','删除成功！');
						
						$.ajax({
							url : GLOBAL.WEBROOT + "/systemMenu/menudelete",
							data : nodes[0],
							datatype:'json',
							success : function(data) {
								if(data != null)
								{
									treeObj.removeNode(nodes[0]);
									ebcForm.resetForm('#menudetail');
								}
								
							}
						});
						
					}
				},{
					caption : '取消',
					callback : $.noop
				}]
			}); 
	    }
	});
	$('#btn_menu_save').click(function(){
		
		var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getSelectedNodes();
		if(!$("#menudetail").valid()) return false;
		var val = ebcForm.formParams($("#menudetail"));
		eDialog.confirm("您确认要保存吗？", {
			buttons : [{
				caption : '确认',
				callback : function(){
					eDialog.alert('success','保存成功！');
					$.ajax({
						url: GLOBAL.WEBROOT+"/systemMenu/savemenu",
				        data:val,
				        type:"post",
				        dataType:"json",
				        async:true,
					    cache:true,
				        success: function (data){
				        	//从后台中获取到数据添加到树结构中
				        	if(data){
				        		if(data.isNew==true){
				        			var newNode = {id:data.id,name:data.name,pId:data.pId};
					        		treeObj.addNodes(nodes[0], newNode);
					        		$('#id').attr('value', data.id);
					        		$('#menu_id').html(data.id);
				        		}else{
				        			nodes[0].name = data.name;
									treeObj.updateNode(nodes[0]);
				        		}
				        		treeObj.refresh();	
				        	}
				        },
				        error:function(XMLHttpRequest, textStatus, errorThrown){
				        	
				        }
					});
				}
			},{
				caption : '取消',
				callback : $.noop
			}]
		});
	
	});
	$('#tree').click(function(){
		return false;
	});
	$('#treeSerach').click(function(evt){
		var menuName =  $("#searchValue").val(); 
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getNodesByParamFuzzy("name", menuName, null);
        //隐藏树
        var allnodes = treeObj.getNodes(); 
        treeObj.hideNodes(allnodes);
        
        for(var i=0;i<nodes.length;i++)
    	{
        	//显示父节点
        	showParentNodes(nodes[i]);
        	treeObj.showNode(nodes[i]);
        	var node = nodes[i].getParentNode();
        	treeObj.expandNode(node, true, true, true);

    	}
	});
	function showParentNodes(nodes)
	{
        var treeObj = $.fn.zTree.getZTreeObj("tree");
    	var node = nodes.getParentNode();
    	if(node != null)
    	{
        	treeObj.showNode(node);
    		showParentNodes(node);
    	}
	}
	function onTreeClick(event, treeId, treeNode, clickFlag) {
		$.ajax({
			url : GLOBAL.WEBROOT+"/systemMenu/onenode",
			type : "POST",
			cache : false,
			timeout : 5000,
			data : {"menu_id":treeNode.id},
			success : function(json) {
			    if(null != json){
			    	//ebcForm.formDataFill('menudetail',json);
			    	$('#menu_id').val(json.id);
			    	$("#menuTitle").val(json.name);
			    	$("#parentmenuID").val(json.pId);
			    	if(json.menuUrl){
			    		$("#menuUrl").val(json.menuUrl);
			    	}
			    	if(json.sort){
			    		$("#sortOrder").val(json.sort);
			    	}
			    	
			    	if(json.menuDesc != null)
			    	{
			    		$('#menuDesc').val(json.menuDesc);
			    	}

			    	
			    	// 执行父节点名称填充。
			    	var pNode = treeNode.getParentNode();
			    	if(pNode){
			    		$('#parentMenuId').val(pNode.name);
			    	}
			    	else
			    	{
			    		$('#parentMenuId').val("root");
			    	}
			    }else{
			    	eDialog.alert('获取菜单信息失败!');
			    }
			}
		});
		$('#btn_menu_save').attr('disabled', false);

	}
	/**	**/	
	var classify = {
			// 树设置.
			setting : {
				
				view:{
					selectedMulti: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				async: {
					enable: true,
					url:GLOBAL.WEBROOT+"/systemMenu/nodedatas",
					type:"post",
					dataType:"json",
					autoParam: ["id"],
					//otherParam:{"form":function(){
					//	return $("#searchText").val();
					//}},
					dataFilter: null
					
				},
				callback: {
					onClick :onTreeClick
					}
			   }
			};
	/**
	var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick :onTreeClick
				}
		};
	
		[
			{"desc":"系统管理","id":4,"isParent":true,"name":"系统管理","pId":"0","parentMenuId":"4","sort":"1"},
{"desc":"菜单管理","id":5,"isParent":false,"menuUrl":"/systemMenu/init","name":"菜单管理","pId":"4","parentMenuId":"5","sort":"1"},
{"desc":"测试","id":23,"isParent":true,"name":"测试","pId":"0","parentMenuId":"23","sort":"null"},
{"desc":"测试12","id":24,"isParent":true,"name":"测试12","pId":"0","parentMenuId":"24","sort":"null"},
{"desc":"测试221","id":25,"isParent":true,"name":"测试221","pId":"0","parentMenuId":"25","sort":"null"}
		];**/

	$.fn.zTree.init($("#tree"), classify.setting);	
});